package com.yjy.pay.rcv;

import com.yjy.pay.check.AbstractChecker;
import com.yjy.pay.check.OrderNoChecker;
import com.yjy.pay.check.OutTxNoChecker;
import com.yjy.pay.content.ApiReq;
import com.yjy.pay.content.PayConent;
import com.yjy.pay.rcv.vo.GatewayPayReq;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/rcv")
public class RcvGatewayController {

    List<AbstractChecker> checkerList = new ArrayList<>();
    public RcvGatewayController(OrderNoChecker orderNoChecker,
                                OutTxNoChecker outTxNoChecker) {
        checkerList.add(orderNoChecker);
        checkerList.add(outTxNoChecker);
    }

    @RequestMapping("/gatewayPay")
    public PayConent gatewayPay(@RequestBody ApiReq<GatewayPayReq> apiReq) {
        PayConent payConent = new PayConent();
        BeanUtils.copyProperties(apiReq, payConent);

        try {
            // 抛出异常时的校验码都是相同的，check(content)校验不通过会返回不同的校验码
            for (AbstractChecker abstractChecker : checkerList) {
                if (!abstractChecker.check(payConent)) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            payConent.setCode("1001");
            payConent.setMsg(e.getMessage());
        }

        return payConent;
    }
}
